package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.RoleType;
import com.cloudfun.campusshare.common.constant.UserCouponStatus;
import com.cloudfun.campusshare.common.model.dto.BatchOperateDTO;
import com.cloudfun.campusshare.common.model.dto.UserCouponCreateDTO;
import com.cloudfun.campusshare.common.model.dto.UserCouponSearchDTO;
import com.cloudfun.campusshare.common.model.vo.UserCouponListVO;
import com.cloudfun.campusshare.common.model.vo.UserCouponStatusStatListVO;
import com.cloudfun.campusshare.common.model.vo.UserCouponVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.common.security.AuthenticationHolder;
import com.cloudfun.campusshare.entity.UserEntity;
import com.cloudfun.campusshare.service.UserCouponService;
import com.cloudfun.campusshare.service.UserRelService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by CodeGenerator on 2020/05/29
 */
@Slf4j
@RestController
@AuthRequired
@RequestMapping("/v1/UserCoupon")
@Api("用户代金券模块")
public class UserCouponController {

    @Autowired
    private UserCouponService userCouponService;

    @Autowired
    private UserRelService userRelService;

    @PostMapping("/issue")
    @ApiOperation(value = "直接向用户发放代金券")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    public BaseResult<?> statusStatisticsList(@ApiParam(value = "createDTO", required = true) @Valid @RequestBody UserCouponCreateDTO createDTO) {
        UserEntity userMerchant = userRelService.getUserMerchant(AuthenticationHolder.getUserId());
        createDTO.setMerchantId(userMerchant.getId());
        createDTO.setIssuerId(AuthenticationHolder.getUserAuth().getUserId());
        createDTO.setIssuerName(AuthenticationHolder.getUserAuth().getUsername());
        userCouponService.create(createDTO);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @GetMapping("/statusStatList")
    @ApiOperation(value = "用户代金券状态统计列表")
    public BaseResult<PageResult<UserCouponStatusStatListVO>> statusStatisticsList(Pageable pageable,
                                                                                   @ApiParam("手机号") @RequestParam(required = false) String phone) {
        PageResult<UserCouponStatusStatListVO> res = userCouponService.statusStatisticsList(pageable, phone);
        return BaseResultFactory.produceResult(Code.SUCCESS, res);
    }

    @GetMapping("/customerCouponList")
    @ApiOperation(value = "运营商获取用户代金券列表")
    public BaseResult<PageResult<UserCouponVO>> omCustomerCouponList(Pageable pageable, @ApiParam("用户id") @RequestParam String userId) {
        UserCouponSearchDTO searchDTO = new UserCouponSearchDTO();
        searchDTO.setUserId(userId);
        return BaseResultFactory.produceResult(Code.SUCCESS, userCouponService.list(pageable, searchDTO));
    }

    @GetMapping("/customer/couponList")
    @ApiOperation(value = "用户分页获取代金券列表")
    @AuthRequired(role = RoleType.CUSTOMER)
    public BaseResult<PageResult<UserCouponVO>> customerCouponPageList(Pageable pageable, UserCouponSearchDTO searchDTO) {
        searchDTO.setUserId(AuthenticationHolder.getUserId());
        return BaseResultFactory.produceResult(Code.SUCCESS, userCouponService.list(pageable, searchDTO));
    }

    @GetMapping("/customer/allCouponList")
    @ApiOperation(value = "用户获取所有代金券列表")
    @AuthRequired(role = RoleType.CUSTOMER)
    public BaseResult<List<UserCouponVO>> customerCouponAllList(UserCouponSearchDTO searchDTO) {
        searchDTO.setUserId(AuthenticationHolder.getUserId());
        return BaseResultFactory.produceResult(Code.SUCCESS, userCouponService.listAll(searchDTO));
    }

    @GetMapping("/customer/maxAmount")
    @ApiOperation(value = "用户获取可用最大金额代金券")
    @AuthRequired(role = RoleType.CUSTOMER)
    public BaseResult<UserCouponListVO> customerCouponCount(UserCouponSearchDTO searchDTO) {
        searchDTO.setUserId(AuthenticationHolder.getUserId());
        searchDTO.setStatus(Lists.newArrayList(UserCouponStatus.NOT_USED));
        return BaseResultFactory.produceResult(Code.SUCCESS, userCouponService.maxAmountCoupon(searchDTO));
    }

    @GetMapping("/customer/couponCount")
    @ApiOperation(value = "用户获取代金券数量")
    @AuthRequired(role = RoleType.CUSTOMER)
    public BaseResult<Long> customerCouponCount() {
        Long res = userCouponService.customerCouponCount(AuthenticationHolder.getUserId());
        return BaseResultFactory.produceResult(Code.SUCCESS, res);
    }

    @GetMapping("/invalid")
    @ApiOperation(value = "作废")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    public BaseResult<?> invalid(@ApiParam("id") @RequestParam String id) {
        userCouponService.invalid(id);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @GetMapping("/invalid/batch")
    @ApiOperation("批量作废")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    BaseResult<?> batchInvalid(@ApiParam(value = "BatchOperateDTO", required = true) @RequestBody BatchOperateDTO<UserCouponSearchDTO> batchOperateDTO) {
        if (CollectionUtils.isEmpty(batchOperateDTO.getIds()) && null == batchOperateDTO.getSearchData() && !BooleanUtils.isTrue(batchOperateDTO.getAll())) {
            return BaseResultFactory.produceEmptyResult(Code.PARAMETER_ERROR, "参数错误");
        }
        Map<Code, Integer> res;
        if (!CollectionUtils.isEmpty(batchOperateDTO.getIds())) {
            res = userCouponService.invalid(batchOperateDTO.getIds());
        } else if (null != batchOperateDTO.getSearchData()) {
            res = userCouponService.invalid(batchOperateDTO.getSearchData());
        } else {
            res = userCouponService.invalidAll();
        }
        return BaseResultFactory.produceResult(Code.SUCCESS, res);
    }

    @PutMapping("/list")
    @ApiOperation("分页列表")
    BaseResult<PageResult<UserCouponListVO>> search(Pageable pageable, @RequestBody UserCouponSearchDTO searchDTO) {
        return BaseResultFactory.produceResult(Code.SUCCESS, userCouponService.search(searchDTO, pageable));
    }
}